package cn.com.wdjedu.androidapp.wjeduandroid.support.lib.settinghelper;

import android.content.Context;
import android.support.annotation.NonNull;

import cn.com.wdjedu.androidapp.wjeduandroid.R;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.encrypt.EncryptAccount;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.encrypt.EncryptBase;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.global.GlobalContext;
import cn.com.wdjedu.androidapp.wjeduandroid.support.lib.pulltorefresh.Utility;


/**
 * User: qii
 * Date: 12-11-28
 */
public class SettingUtility {

    private static final String FIRSTSTART = "firststart";
    private static final String LAST_FOUND_WEIBO_ACCOUNT_LINK = "last_found_weibo_account_link";
    private static final String BLACK_MAGIC = "black_magic";
    private static final String CLICK_TO_TOP_TIP = "click_to_top_tip";


    public static final String SOUND = "sound";

    public static final String AUTO_REFRESH = "auto_refresh";

    //appearance
    public static final String THEME = "theme";
    public static final String LIST_AVATAR_MODE = "list_avatar_mode";
    public static final String LIST_PIC_MODE = "list_pic_mode";
    public static final String LIST_HIGH_PIC_MODE = "list_high_pic_mode";
    public static final String LIST_FAST_SCROLL = "list_fast_scroll";
    public static final String FONT_SIZE = "font_size";
    public static final String SHOW_BIG_PIC = "show_big_pic";
    public static final String SHOW_BIG_AVATAR = "show_big_avatar";

    //read
    public static final String READ_STYLE = "read_style";

    //notification
    public static final String FREQUENCY = "frequency";
    public static final String ENABLE_FETCH_MSG = "enable_fetch_msg";
    public static final String DISABLE_FETCH_AT_NIGHT = "disable_fetch_at_night";
    public static final String ENABLE_VIBRATE = "vibrate";
    public static final String ENABLE_LED = "led";
    public static final String ENABLE_RINGTONE = "ringtone";
    public static final String JBNOTIFICATION_STYLE = "jbnotification";
    public static final String ENABLE_MENTION_TO_ME = "mention_to_me";
    public static final String ENABLE_COMMENT_TO_ME = "comment_to_me";
    public static final String ENABLE_MENTION_COMMENT_TO_ME = "mention_comment_to_me";

    //filter
    public static final String FILTER = "filter";

    //traffic control
    public static final String UPLOAD_PIC_QUALITY = "upload_pic_quality";
    public static final String COMMENT_REPOST_AVATAR = "comment_repost_list_avatar";
    public static final String SHOW_COMMENT_REPOST_AVATAR = "show_comment_repost_list_avatar";
    public static final String DISABLE_DOWNLOAD_AVATAR_PIC = "disable_download";
    public static final String MSG_COUNT = "msg_count";
    public static final String WIFI_UNLIMITED_MSG_COUNT = "enable_wifi_unlimited_msg_count";
    public static final String WIFI_AUTO_DOWNLOAD_PIC = "enable_wifi_auto_download_pic";

    //performance
    public static final String DISABLE_HARDWARE_ACCELERATED
            = "pref_disable_hardware_accelerated_key";

    //other
    public static final String ENABLE_INTERNAL_WEB_BROWSER = "enable_internal_web_browser";
    public static final String ENABLE_CLICK_TO_CLOSE_GALLERY = "enable_click_to_close_gallery";
    public static final String CLICK_TO_CLEAN_CACHE = "click_to_clean_cache";
    public static final String FILTER_SINA_AD = "filter_sina_ad";

    //about
    public static final String OFFICIAL_WEIBO = "pref_official_weibo_key";
    public static final String SUGGEST = "pref_suggest_key";
    public static final String VERSION = "pref_version_key";
    public static final String RECOMMEND = "pref_recommend_key";
    public static final String DONATE = "pref_donate_key";
    public static final String CACHE_PATH = "pref_cache_path_key";
    public static final String SAVED_PIC_PATH = "pref_saved_pic_path_key";
    public static final String SAVED_LOG_PATH = "pref_saved_log_path_key";
    public static final String AUTHOR = "pref_author_key";
    public static final String DEBUG_MEM_INFO = "pref_mem_key";
    public static final String CRASH = "pref_crash_key";

    private static final String RECORD_WEBSITE_NAME = "website_name_key";   /*登录网点-名称*/
    private static final String RECORD_WEBSITE_VALUE = "website_value_key"; /*登录网点-值*/
    private static final String LOCAL_ACCOUNT = "local_account"; /*记录的账号*/


    private SettingUtility() {

    }

    public static void setDefaultAccountId(String id) {
        SettingHelper.setEditor(getContext(), "id", id);
    }

    public static String getDefaultAccountId() {
        return SettingHelper.getSharedPreferences(getContext(), "id", "");
    }

    private static Context getContext() {
        return GlobalContext.getInstance();
    }

    public static boolean firstStart() {
        boolean value = SettingHelper.getSharedPreferences(getContext(), FIRSTSTART, true);
        if (value) {
            SettingHelper.setEditor(getContext(), FIRSTSTART, false);
        }
        return value;
    }

    public static boolean isEnableFilter() {
        return SettingHelper.getSharedPreferences(getContext(), FILTER, false);
    }

    public static int getFontSize() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), FONT_SIZE, "15");
        return Integer.valueOf(value);
    }

    public static int getAppTheme() {
        String value = SettingHelper.getSharedPreferences(getContext(), THEME, "1");

        switch (Integer.valueOf(value)) {
            case 1:
                return R.style.AppTheme_Light;

            case 2:
                return R.style.AppTheme_Light;

            default:
                return R.style.AppTheme_Light;
        }
    }

    public static void switchToAnotherTheme() {
        String value = SettingHelper.getSharedPreferences(getContext(), THEME, "1");
        switch (Integer.valueOf(value)) {
            case 1:
                SettingHelper.setEditor(getContext(), THEME, "2");
                break;
            case 2:
                SettingHelper.setEditor(getContext(), THEME, "1");
                break;
            default:
                SettingHelper.setEditor(getContext(), THEME, "1");
                break;
        }
    }

    public static int getHighPicMode() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), LIST_HIGH_PIC_MODE, "2");
        return Integer.valueOf(value);
    }

    public static int getCommentRepostAvatar() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), COMMENT_REPOST_AVATAR, "1");
        return Integer.valueOf(value);
    }

    public static int getListAvatarMode() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), LIST_AVATAR_MODE, "1");
        return Integer.valueOf(value);
    }

    public static int getListPicMode() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), LIST_PIC_MODE, "1");
        return Integer.valueOf(value);
    }

    public static void setEnableCommentRepostAvatar(boolean value) {
        SettingHelper.setEditor(getContext(), SHOW_COMMENT_REPOST_AVATAR, value);
    }

    public static boolean getEnableCommentRepostListAvatar() {
        return SettingHelper
                .getSharedPreferences(getContext(), SHOW_COMMENT_REPOST_AVATAR,
                        true);
    }

    public static int getNotificationStyle() {
        String value = SettingHelper
                .getSharedPreferences(getContext(), JBNOTIFICATION_STYLE, "1");

        switch (Integer.valueOf(value)) {
            case 1:
                return 1;

            case 2:
                return 2;

            default:
                return 1;
        }
    }

    public static boolean isEnablePic() {
        return !SettingHelper
                .getSharedPreferences(getContext(), DISABLE_DOWNLOAD_AVATAR_PIC,
                        false);
    }

    public static boolean getEnableBigPic() {
        return SettingHelper
                .getSharedPreferences(getContext(), SHOW_BIG_PIC, false);
    }

    public static boolean getEnableFetchMSG() {
        return SettingHelper
                .getSharedPreferences(getContext(), ENABLE_FETCH_MSG, false);
    }

    public static boolean getEnableAutoRefresh() {
        return SettingHelper
                .getSharedPreferences(getContext(), AUTO_REFRESH, false);
    }

    public static boolean getEnableBigAvatar() {
        return SettingHelper
                .getSharedPreferences(getContext(), SHOW_BIG_AVATAR, false);
    }

    public static boolean getEnableSound() {
        return SettingHelper.getSharedPreferences(getContext(), SOUND, true)
                && Utility.isSystemRinger(getContext());
    }

    public static boolean disableFetchAtNight() {
        return SettingHelper
                .getSharedPreferences(getContext(), DISABLE_FETCH_AT_NIGHT, true)
                && Utility.isSystemRinger(getContext());
    }

    public static String getFrequency() {
        return SettingHelper.getSharedPreferences(getContext(), FREQUENCY, "1");
    }

    public static void setEnableBigPic(boolean value) {
        SettingHelper.setEditor(getContext(), SHOW_BIG_PIC, value);
    }

    public static void setEnableBigAvatar(boolean value) {
        SettingHelper.setEditor(getContext(), SHOW_BIG_AVATAR, value);
    }

    public static void setEnableFilter(boolean value) {
        SettingHelper.setEditor(getContext(), FILTER, value);
    }

    public static void setEnableFetchMSG(boolean value) {
        SettingHelper.setEditor(getContext(), ENABLE_FETCH_MSG, value);
    }

    public static boolean allowVibrate() {
        return SettingHelper
                .getSharedPreferences(getContext(), ENABLE_VIBRATE, false);
    }

    public static boolean allowLed() {
        return SettingHelper.getSharedPreferences(getContext(), ENABLE_LED, false);
    }

    public static String getRingtone() {
        return SettingHelper
                .getSharedPreferences(getContext(), ENABLE_RINGTONE, "");
    }

    /**
     * 设置旋转网点记录
     *
     * @param name  网点名称
     * @param value 网点标识值
     */
    public static void setWebSiteLocal(String name, String value) {
        SettingHelper.setEditor(getContext(), RECORD_WEBSITE_NAME, name);
        SettingHelper.setEditor(getContext(), RECORD_WEBSITE_VALUE, value);
    }

    /**
     * 获取选择网点记录
     *
     * @return string[0]=name, string[1]=value
     */
    public static String[] getWebsiteLocal() {
        String name = SettingHelper.getSharedPreferences(getContext(),
                RECORD_WEBSITE_NAME, "");
        String value = SettingHelper.getSharedPreferences(getContext(),
                RECORD_WEBSITE_VALUE, "");
        return new String[]{name, value};
    }

    /**
     * 保存登录过的账号
     */
    public static void saveLocalAccount(@NonNull String account) {
        EncryptBase encryptBase = new EncryptAccount();
        SettingHelper.setEditor(getContext(), LOCAL_ACCOUNT,
                encryptBase.encrypt(account));
    }

    /**
     * 读取保存的账号
     */
    public static String getLocalAccount() {
        String account = SettingHelper.getSharedPreferences(getContext(),
                LOCAL_ACCOUNT, "");
        EncryptBase encryptBase = new EncryptAccount();
        return encryptBase.decrypt(account);
    }

}
